package edu.guigu.appquickstart.adapterview.subitem;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import edu.guigu.appquickstart.R;
import edu.guigu.appquickstart.adapterview.bean.UserBean;

public class ListviewActivity extends AppCompatActivity {

    private ListView listView;          // View
    private MyAdapter2 adapter;          // Cont...
    private ArrayList<UserBean> datas;   // Model

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);
        // Actionbar
        setSupportActionBar(findViewById(R.id.toolbar));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // 1 准备数据
        datas = new ArrayList<UserBean>();
        for (int i = 0; i < 50; i++) {
            final String url = "https://avatars.githubusercontent.com/u/"+(i+1)+"?v=4";
            UserBean userBean = new UserBean("张三" + (i+1), url);
            datas.add(userBean);
        }
        // 2 准备适配器
        adapter = new MyAdapter2(this);
        // 3 数据交给适配器
        adapter.setDataList(datas);
        // 4 listview设置adapter
        listView = findViewById(R.id.listview);
        listView.setAdapter(adapter);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}

// getview()未做优化处理
class MyAdapter1 extends BaseAdapter {
    private Context context;

    public MyAdapter1(Context context) {
        this.context = context;
    }

    // 数据----------------------------------------------------------
    private ArrayList<UserBean> dataList = new ArrayList();

    public void setDataList(ArrayList<UserBean> dataList) {
        this.dataList = dataList;
    }

    public ArrayList<UserBean> getDataList() {
        return dataList;
    }

    // 数据适配到View----------------------------------------------------------
    // 数量
    @Override
    public int getCount() {
        return dataList.size();
    }

    // 当前位置的数据
    @Override
    public UserBean getItem(int position) {
        return dataList.get(position);
    }

    // 当前位置的id(唯一)
    @Override
    public long getItemId(int position) {
        return position;
    }

    // 当前位置的视图
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 加载xml布局  new  LinearLayout()  new ImageView() new TextView()
        convertView = LayoutInflater.from(context).inflate(R.layout.item_list_, parent, false);
        // 拿到布局上和各个子控件
        ImageView imageView = convertView.findViewById(R.id.imageview);
        TextView textView = convertView.findViewById(R.id.textview);
        // 数据更新到当前布局的对应子控件上
        UserBean userBean = getItem(position);
        textView.setText(userBean.getLogin());

        // 返回当前位置的视图
        return convertView;
    }
}

// getView()做出了优化处理
class MyAdapter2 extends BaseAdapter {
    private Context context;

    public MyAdapter2(Context context) {
        this.context = context;
    }

    // 数据----------------------------------------------------------
    private ArrayList<UserBean> dataList = new ArrayList();

    public void setDataList(ArrayList<UserBean> dataList) {
        this.dataList = dataList;
    }

    public ArrayList<UserBean> getDataList() {
        return dataList;
    }

    // 数据适配到View----------------------------------------------------------
    // 数量
    @Override
    public int getCount() {
        return dataList.size();
    }

    // 当前位置的数据
    @Override
    public UserBean getItem(int position) {
        return dataList.get(position);
    }

    // 当前位置的id(唯一)
    @Override
    public long getItemId(int position) {
        return position;
    }

    // 当前位置的视图
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolde viewHolde = null;

        if (convertView == null) {
            // 加载xml布局  new  LinearLayout()  new ImageView() new TextView()
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_, parent, false);
            // 拿到布局上和各个子控件, 封装成ViewHolder对象，且将它关联存放到convertView中
            viewHolde = new ViewHolde(convertView);
            convertView.setTag(viewHolde);
        } else {
            // 如果convertView不是null的,说明是在重复利用
            // 从convertView中取出ViewHolder对象
            viewHolde = (ViewHolde) convertView.getTag();
        }

        // 数据更新
        UserBean userBean = getItem(position);
        viewHolde.textView.setText(userBean.getLogin());
        Glide.with(context)
                .load(userBean.getAvatar_url())
                .placeholder(R.drawable.ic_loading)
                .error(R.drawable.ic_image)
                .override(60,60)
                .centerCrop()
                .into(viewHolde.imageView);

        return convertView;
    }

    private class ViewHolde {
        ImageView imageView = null;
        TextView textView = null;

        public ViewHolde(View convertView) {
            imageView = convertView.findViewById(R.id.imageview);
            textView = convertView.findViewById(R.id.textview);
        }
    }
}